﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRM.BIZ;
using HRM.Entity;
using HRM.Utility;
using System.IO;

namespace WebUI.UserControls
{
    public partial class EmployeeDetails : System.Web.UI.UserControl
    {
        private static int ID = -1;
        private static string picLink = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadFormData();
            }
        }

        private void LoadRelationStatusType()
        {
            var relationshipTypes = new RelationshipStatusTypeBiz().GetAll();
            ddlRelationshipStatus.DataSource = relationshipTypes;
            ddlRelationshipStatus.DataTextField = "Name";
            ddlRelationshipStatus.DataValueField = "ID";
            ddlRelationshipStatus.DataBind();
        }

        private void LoadEducationLevel()
        {
            var educationLevels = new EducationLevelBiz().GetAll();
            ddlEducationLevel.DataSource = educationLevels;
            ddlEducationLevel.DataTextField = "Name";
            ddlEducationLevel.DataValueField = "ID";
            ddlEducationLevel.DataBind();
        }

        private void LoadPosition()
        {
            var positions = new PositionBiz().GetAll();
            ddlPosition.DataSource = positions;
            ddlPosition.DataTextField = "Name";
            ddlPosition.DataValueField = "ID";
            ddlPosition.DataBind();
        }

        private void LoadDepartment()
        {
            var departments = new DepartmentBiz().GetAll();
            ddlDepartment.DataSource = departments;
            ddlDepartment.DataTextField = "Name";
            ddlDepartment.DataValueField = "ID";
            ddlDepartment.DataBind();
        }

        public void LoadFormData()
        {
            LoadRelationStatusType();
            LoadEducationLevel();
            LoadPosition();
            LoadDepartment();
        }

        public Employee CurrentEmployeeDetails
        {
            get
            {                
                return new Employee()
                {
                    ID = ID,
                    Name = txtEmpName.Text,
                    EmpID = txtEmpID.Text,
                    DOB = txtDOB.Text.ToDateTime("dd/MM/yyyy"),
                    Sex = rblSex.SelectedValue.ToInt32(),
                    IDCardNo = txtIDCardNo.Text,
                    IDCardIssuedDate = txtIDCardIssuedDate.Text.ToDateTime("dd/MM/yyyy"),
                    RelationshipStatus = ddlRelationshipStatus.SelectedValue.ToInt32(),
                    NumOfChild =txtNumOfChild.Text.ToInt32(),
                    EducationLevel = ddlEducationLevel.SelectedValue.ToInt32(),
                    BaseSalary = txtBaseSalary.Text.ToInt32(),
                    Position = ddlPosition.SelectedValue.ToInt32(),
                    Department = ddlDepartment.SelectedValue.ToInt32(),
                    PictureLink = GetDestinationEmployeePicturePath(),
                    StartedDate = txtStartedDate.Text.ToDateTime("dd/MM/yyyy"),
                    Status = chkActive.Checked
                };
            }
            set
            {
                ID = value.ID;
                picLink = value.PictureLink;

                txtEmpID.Text = value.EmpID;
                txtEmpName.Text = value.Name;
                txtDOB.Text = value.DOB.ToString("dd/MM/yyyy");
                rblSex.SelectedValue = value.Sex.ToString();
                txtIDCardNo.Text = value.IDCardNo;
                txtIDCardIssuedDate.Text = value.IDCardIssuedDate.ToString("dd/MM/yyyy");
                ddlRelationshipStatus.SelectedValue = value.RelationshipStatus.ToString();
                txtNumOfChild.Text = value.NumOfChild.ToString();
                ddlEducationLevel.SelectedValue = value.EducationLevel.ToString();
                txtBaseSalary.Text = value.BaseSalary.ToString();
                ddlPosition.SelectedValue = value.Position.ToString();
                ddlDepartment.SelectedValue = value.Department.ToString();
                txtStartedDate.Text = value.StartedDate.ToString("dd/MM/yyyy");
                chkActive.Checked = value.Status;
            }
        }

        private string GetDestinationEmployeePicturePath()
        {
            if (fuPicture.HasFile)
            {
                string fileExt = Path.GetExtension(fuPicture.FileName);
                string uploadFolder = Common.GetAppSettingValue("UploadEmployeePictureFolder");
                string fileName = Guid.NewGuid().ToString() + fileExt;

                if (!uploadFolder.EndsWith(@"\"))
                    uploadFolder += @"\";
                fileName = uploadFolder + fileName;

                return fileName;
            }

            return picLink;
        }

        public void UploadEmployeePicture(string path)
        {
            string uploadFolder = Common.GetAppSettingValue("UploadEmployeePictureFolder");
            if (!Directory.Exists(uploadFolder))
                Directory.CreateDirectory(uploadFolder);

            if (fuPicture.HasFile)
            {
                fuPicture.SaveAs(path);
            }
        }

        public bool Enable
        {
            set
            {
                pnlContainer.Enabled = value;
                trPicture.Visible = false;
            }
        }
    }
}